package com.rh.bmw4s.ui;

import android.app.DialogFragment;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.view.CommonHeader;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.rh.bmw4s.R;
import com.rh.bmw4s.adapter.CategoryDetailAdapter;
import com.rh.bmw4s.adapter.CatergoryAdapter;
import com.rh.bmw4s.bean.BaseBean;
import com.rh.bmw4s.bean.BrandBean;
import com.rh.bmw4s.comm.Constant;
import com.rh.bmw4s.utills.DealResult;
import com.rh.bmw4s.utills.DialogUtils;
import com.rh.bmw4s.utills.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/9/26.
 */

public class CaregoryDetailFragment extends DialogFragment {
//    @BindView(R.id.tv_back)
    TextView tvBack;
    Unbinder unbinder;
    private float startY = 0;
    private float currentY = 0;
    //    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    private CategoryDetailAdapter adapter;
    private LRecyclerViewAdapter lrecyclerViewAdapter;

    @Override
    public void onStart() {
        super.onStart();
        //获取屏幕信息
//        DisplayMetrics dm = new DisplayMetrics();
//        getActivity().getWindowManager().getDefaultDisplay().getMetrics( dm );
//        getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height );
        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        Window window = getDialog().getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setGravity(Gravity.BOTTOM | Gravity.RIGHT);
        window.setLayout((int) (metrics.widthPixels * 0.8), (int) (metrics.heightPixels * 0.8));
        //设置布局部分的背景色
        window.setBackgroundDrawableResource(R.color.colorAccent);
//        window.setBackgroundDrawable(new ColorDrawable(000000));
        //整体背景色(设置透明度)
//        window.setDimAmount(0.8f);
        //动画
        window.setWindowAnimations(R.style.BottmoDialogAnim);
//        getDialog().setCanceledOnTouchOutside(false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo_Light);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.fragment_category_detail, container);
        recyclerView=view.findViewById(R.id.recyclerView);
        tvBack=view.findViewById(R.id.tv_back);
//        unbinder = ButterKnife.bind(this, view);
        Bundle bundle = getArguments();
        int id = bundle.getInt("id");
        initRecyclerView();
        getDatas(id);
        //滑动取消
        return view;
    }

    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new CategoryDetailAdapter(getActivity());
        adapter.setDataList(new ArrayList<BrandBean>());
        lrecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        recyclerView.setAdapter(lrecyclerViewAdapter);
//        CommonHeader header = new CommonHeader(getActivity(), R.layout.head_category_grid);
//        lRecyclerViewAdapter.addHeaderView(header);
        DividerDecoration divider = new DividerDecoration.Builder(getActivity())
                .setHeight(R.dimen.divider_height_1)
                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.line_1)
                .build();
        recyclerView.addItemDecoration(divider);
        recyclerView.setLoadMoreEnabled(false);
        recyclerView.setPullRefreshEnabled(false);
        recyclerView.refresh();
        recyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.d("CCC",startY+"^^^^"+motionEvent);
                recyclerView.onInterceptTouchEvent(motionEvent);
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        Log.d("CCC",startY+"---");
                        startY = motionEvent.getX();
                        break;
                    case MotionEvent.ACTION_UP:
                        currentY = motionEvent.getX();

                        if (currentY - startY > 0 && (Math.abs(currentY - startY) > 100)) {
                            //向下滑动
                            Log.d("CCC",Math.abs(currentY - startY) +"****");
                            getDialog().dismiss();

                        }
                        break;
                    case MotionEvent.ACTION_MOVE:

                        break;
                }
                return true;
            }
        });


//        recyclerView.onTouchEvent(new MotionEvent(){})
    }

    private void getDatas(int id) {
        OkGo.<String>post(Constant.SELECTION_DETAIL)
                .tag(this)
                .params("id", id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        BaseBean<BrandBean> bean = DealResult.getInstace().dealData(getActivity(), response, new TypeToken<BaseBean<BrandBean>>() {
                        }.getType());
                        List<BrandBean> beans = bean.getDatas();
                        if(beans==null){
                            return;
                        }
                        setDatas(beans);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(getActivity(), getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    private void setDatas(List<BrandBean> beans) {
        adapter.setDataList(beans);
        recyclerView.refresh();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        unbinder.unbind();
    }

    @OnClick(R.id.tv_back)
    public void onViewClicked() {
        getDialog().dismiss();
    }


    //定义接口
    public interface slideDialogOnClick {
        void closeDialog();

        void buyNow();
    }

    private slideDialogOnClick slideDialogOnClick;

    /*public void setSlideDialogOnClick(ParamListDialog.slideDialogOnClick slideDialogOnClick) {
        this.slideDialogOnClick = slideDialogOnClick;
    }*/
}
